package com.zq.zqplayer.mvp.main.ui

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.ziq.base.base.BaseFragment
import com.ziq.base.extension.binding
import com.zq.customviewlib.AutoRollViewPager
import com.zq.zqplayer.R
import com.zq.zqplayer.bean.RoomInfoBean
import com.zq.zqplayer.databinding.FragmentRecommendBinding
import com.zq.zqplayer.mvp.adapter.RecommendAdapter
import com.zq.zqplayer.mvp.live.ui.LiveActivity
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class RecommendFragment : BaseFragment(R.layout.fragment_recommend) {

    private val binding : FragmentRecommendBinding by binding()

    val mViewModel: RecommendViewModel by viewModels()

    var data:ArrayList<RoomInfoBean> = arrayListOf()
    var adapter:RecommendAdapter? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.root
        initData(view, savedInstanceState)
        mViewModel.getLiveList()
    }

    fun initData(view: View, savedInstanceState: Bundle?) {
        initBanner()
        adapter = RecommendAdapter(context,data)
        binding.recycleView.adapter = adapter // 要先设置 adapter  SpanSizeLookup才有效
        val layoutManager: GridLayoutManager = GridLayoutManager(context, 2)
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return 1
            }
        }
        binding.recycleView.layoutManager = layoutManager
        adapter!!.mOnActionListener = object : RecommendAdapter.OnActionListener {
            override fun onLiveItemClick(item: RoomInfoBean) {
                LiveActivity.openVideo(context, item.live_url, item.room_name)
            }
        }


        binding.smartRefreshLayout.setOnRefreshListener {

        }
        binding.smartRefreshLayout.setOnLoadMoreListener {

        }
        binding.smartRefreshLayout.autoRefresh()
    }

    private fun initBanner() {
        val recommendBannerList:ArrayList<Int> = arrayListOf()
        recommendBannerList.add(R.drawable.home_recommend_live_app_1523155786)
        recommendBannerList.add(R.drawable.home_recommend_live_app_1526869950)
        recommendBannerList.add(R.drawable.home_recommend_live_app_1530243925)
        recommendBannerList.add(R.drawable.home_recommend_live_app_1540959428)
        val autoRollViewPager: AutoRollViewPager = view?.findViewById(R.id.autoRollViewPager)!!
        autoRollViewPager.adapter = object : AutoRollViewPager.RollViewPagerAdapter<Int>(recommendBannerList) {
            override fun getItemLayoutRes(): Int {
                return R.layout.item_in_recommend_banner
            }

            override fun onBindItemView(rootView: ViewGroup?, position: Int, realPosition: Int) {
                val imageUrl:Int = getItem(realPosition)
                val ivContent: ImageView = rootView!!.findViewById(R.id.iv_content)
                ivContent.setImageResource(imageUrl)
            }
        }
        autoRollViewPager.startRoll()
    }





}